/**
* Database Connection
*/
var Models = require('./Models');

module.exports = function(options) {
    var database = {
        options: options
    };
    
    var Sequelize = require('sequelize');
    var passwordHash = require('password-hash');
    
    database.module = Sequelize;
    database.client = new Sequelize(options.DATABASE, options.USER, options.PASS, {
        host: options.HOST,
        port: options.PORT,
        logging: options.logging,
        dialect: 'mysql',
        maxConcurrentQueries: 100
    });

    var User = database.client.import(__dirname + "/models/User");

    Models.add('User', User);
    
    database.client.sync({force: true}).on('success', function() {
    	User.create({
			login : 'Admin',
			password : passwordHash.generate('Admin123')
		}).error(function(error) {}).success(function(user){
			console.log('Création du compte admin : Admin/Admin123');
		});
        User.create({
            login : 'test',
            password : passwordHash.generate('titi')
        }).error(function(error) {}).success(function(user){
            console.log('Création du compte admin : test/titi');
        });
	});
    
    return database;
};